/*
 * This is the controller class of the CD Player
 */
package edu.iyte.ceng431.hw03.cdplayer.controller;

import edu.iyte.ceng431.hw03.cdplayer.model.CDPlayer;
import edu.iyte.ceng431.hw03.cdplayer.model.PlayingCD;
import edu.iyte.ceng431.hw03.cdplayer.view.MainFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author hp
 */
public class CDPlayerController {

    public CDPlayer cdPlayer;
    public MainFrame mainFrame;
    public CDPlayerController(CDPlayer cdPlayer, MainFrame mainFrame) {
        this.cdPlayer = cdPlayer;
        this.mainFrame = mainFrame;
        cdPlayer.addObservers(mainFrame);
        mainFrame.addPreviousButtonActionPerformed(new PreviousListener());
        mainFrame.addPlayButtonActionPerformed(new PlayPauseListener());
        mainFrame.addNextButtonActionPerformed(new NextListener());
        mainFrame.addStopButtonActionPerformed(new StopListener());
        mainFrame.addEjectButtonActionPerformed(new LoadEjectListener());
        mainFrame.addLoadButtonActionPerformed(new LoadFileListener());
        mainFrame.addVolumeSliderChanged(new VolumeSliderChangeListener());
        mainFrame.setVisible(true);
        mainFrame.setLocationRelativeTo(null);
    }

    class PreviousListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            cdPlayer.previousTrack();
        }
    }

    class PlayPauseListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            cdPlayer.playPause();
        }
    }

    class NextListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            cdPlayer.nextTrack();
        }
    }

    class StopListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            cdPlayer.stop();
        }
    }

    class LoadEjectListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            cdPlayer.load();
        }
    }

    class VolumeSliderChangeListener implements ChangeListener {

        public void stateChanged(ChangeEvent e) {
            mainFrame.getVolumeLabel().setText(String.valueOf(mainFrame.getVolumeSlider().getValue()));
            if(mainFrame.getVolumeSlider().getValue() == 0)
            {
                mainFrame.getVolumeLabel().setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/iyte/ceng431/hw03/cdplayer/view/images/mute.png")));
            }
            else
            {
                mainFrame.getVolumeLabel().setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/iyte/ceng431/hw03/cdplayer/view/images/volume.png")));
            }
        }
    }

    class LoadFileListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            chooser.setCurrentDirectory(new File("./src/edu/iyte/ceng431/hw03/cdplayer/controller"));
            chooser.removeChoosableFileFilter(chooser.getFileFilter());
            chooser.setFileFilter(new FileNameExtensionFilter(".CD Files", "cd"));
            chooser.showOpenDialog(mainFrame);
            String name = null;
            if (chooser.getSelectedFile() != null) {
                name = chooser.getSelectedFile().getAbsolutePath();
                PlayingCD myCD = CDReader.readCDFile(name);
                if (myCD != null) {
                    myCD.addObserver(mainFrame);//notify?
                    cdPlayer.setCd(myCD);
                }
            } else {
                cdPlayer.setCd(null);
            }
        }
    }
}
